package com.comtrade.ilserver.tasks;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;
/** 
 * BeaconServer class represent a beacon in space <br/>
 * example json:<br/>
 * 
 * {<br/>
 * "_id":13, <br/>
 * "_uuid":"dec43c9d-04ed-4694-9a11-7ec1c85676e4", <br/>
 * "_version":0, <br/>
 * "macAddress":"CF:A2:1C:85:CD:60", <br/>
 * "x":260.0, <br/>
 * "y":460.0, <br/>
 * "z":100.0, <br/>
 * "angle":90, <br/>
 * "message":"CF:A2:1C:85:CD:60", <br/>
 * "spaceId":640 <br/>}
 *
 */



/////// OBAVEZNO PROMENITI DA SA SERVERA KOORDINATE PROMENI U METRE A NE DA HVATA CM
public class BeaconServer implements Parcelable{
	private static final String IdForPars="_id";
	private static final String uuidForPars="_uuid";
	private static final String versionForPars="_version";
	private static final String macForPars="macAddress";
	private static final String xForPars="x";
	private static final String yForPars="y";
	private static final String zForPars="z";
	private static final String messageForPars="message";
	private static final String spaceIdForPars="spaceId";

	private int _id,spaceId,_version;
	private String _uuid,message,macAddress;
	private double x,y,z;
	
	public BeaconServer(int _id, String _uuid, int _version, int x, int y, int z,
			String description, int spaceId, String mac) {
		super();
		this._id=_id;
		this._uuid=_uuid;
		this._version=_version;
		this.macAddress=mac;
		this.x=((double)x)/100;
		this.y=((double)y)/100;
		this.z=((double)z)/100;
		this.message=description;
		this.spaceId=spaceId;

	}
	
	public BeaconServer(Parcel source){
		this._id=source.readInt();
		this._uuid=source.readString();
		this._version=source.readInt();
		this.macAddress=source.readString();
		this.x=source.readInt()/100.0;
		this.y=source.readInt()/100.0;
		this.z=source.readInt()/100.0;
		this.message=source.readString();
		this.spaceId=source.readInt();
	}

	
	public static BeaconServer fromJSON(String json){
		try {
			JSONObject jsonObj = new JSONObject(json);
			int _id=jsonObj.getInt(IdForPars);
			String _uuid=jsonObj.getString(uuidForPars);
			int _version= jsonObj.getInt(versionForPars);
			String mac=jsonObj.getString(macForPars);
			int x=jsonObj.getInt(xForPars);
			int y=jsonObj.getInt(yForPars);
			int z=jsonObj.getInt(zForPars);
			String message=jsonObj.getString(messageForPars);
			int spaceId=jsonObj.getInt(spaceIdForPars);
			return new BeaconServer( _id, _uuid, _version, x, y, z,
					message, spaceId, mac);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	

	
	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		dest.writeInt(this._id);
		dest.writeString(this._uuid);
		dest.writeInt(this._version);
		dest.writeString(this.macAddress);
		dest.writeInt((int)(this.x*100));
		dest.writeInt((int)(this.y*100));
		dest.writeInt((int)(this.z*100));
		dest.writeString(this.message);
		dest.writeInt(this.spaceId);
	}
	
	public int get_id() {
		return _id;
	}

	public void set_id(int _id) {
		this._id = _id;
	}

	public int getSpaceId() {
		return spaceId;
	}

	public void setSpaceId(int spaceId) {
		this.spaceId = spaceId;
	}

	public int get_version() {
		return _version;
	}

	public void set_version(int _version) {
		this._version = _version;
	}

	public String get_uuid() {
		return _uuid;
	}

	public void set_uuid(String _uuid) {
		this._uuid = _uuid;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getMacAddress() {
		return macAddress;
	}

	public void setMacAddress(String macAddress) {
		this.macAddress = macAddress;
	}

	public double getX() {
		return x;
	}

	public void setX(double x) {
		this.x = x;
	}

	public double getY() {
		return y;
	}

	public void setY(double y) {
		this.y = y;
	}

	public double getZ() {
		return z;
	}

	public void setZ(double z) {
		this.z = z;
	}

	public static Parcelable.Creator<BeaconServer> CREATOR = new Parcelable.Creator<BeaconServer>() {

		@Override
		public BeaconServer[] newArray(int size) {
			return new BeaconServer[size];
		}

		@Override
		public BeaconServer createFromParcel(Parcel source) {
			return new BeaconServer(source);
		}
	};

	
	

}
